layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        upload = layui.upload,
        layedit = layui.layedit,
        laydate = layui.laydate,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;
    //绑定校验
    form.verify({
        jobNumber : function(val){
            if( !val || val == ''){
                return "员工ID不能为空";
            }
        },
        montant : function(val){
            if( !val || val == ''){
                return "金额不能为空";
            }
            var checkRule = /^\d{1,13}(?:\.\d{1,2})?$/;
            if( !checkRule.test( val)){
                return "金额最多只能有两位小数";
            }
            if( parseFloat( val) > 50000 || parseFloat( val) < 0.01){
                return "金额范围在0.01-50000之间";
            }
        }
    })

    //加载员工列表
    function loadUserSelect() {
        $.get( "/userAdmin/listForSelect",function( res){
            var users = res;
            var sel = $("select[name=jobNumber]");
            sel.append( "<option value=''>请选择</option> ")
            for( var i = 0 ; i < users.length ; i++){
                var user = users[i];
                sel.append( "<option value=\"" + user.jobNumber + "\">" + user.jobNumber + "</option> ")
            }
            //更新渲染
            form.render( "select");
            sel.val( $("#jobNumberId").val());
            form.render("select");
        });
    }

    loadUserSelect();
    //加载收款方式列表
    function loadMethodeSelect() {
        $.get( "/dataDefine/list/RECHARGE_TYPE",function( res) {
            var methodes = res.data;
            var sel = $("select[name=methode]");
            for (var i = 0; i < methodes.length; i++) {
                var methode = methodes[i];
                sel.append("<option value=\"" + methode.dataKey + "\">" + methode.dataValue + "</option> ")
            }
            //更新渲染
            form.render("select");
            sel.val( $("#methodeId").val());
            form.render("select");
        });
    }

    loadMethodeSelect();

    //监听提交
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //提交数据
        $.post("/recuPerso/addOrUpdate",{
            id : data.field.id,
            contrepartie : data.field.contrepartie,
            jobNumber : data.field.jobNumber,
            methode : data.field.methode,
            memo : data.field.memo,
            status : data.field.status,
            montant : data.field.montant
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });
});
